//
//  JCKSubWebViewController.m
//  Builder
//
//  Created by JCK on 2017/12/20.
//  Copyright © 2017年 JCK. All rights reserved.
//

#import "JCKSubWebViewController.h"
#import <WebKit/WebKit.h>
#import "JCKNoHearViewController.h"
#import "SVProgressHUD.h"
#import "BaseSocketManager.h"
@interface JCKSubWebViewController ()<WKNavigationDelegate,UICollectionViewDelegate,UINavigationControllerDelegate>
{
    WKWebView *weWebView;
}

@end

@implementation JCKSubWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloData:) name:Socket1003 object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloData:) name:Socket1006 object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloData:) name:Socket1008 object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloData:) name:Socket1014 object:nil];
    
    [SVProgressHUD showWithStatus:@"加载中..."];
    weWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
    weWebView.navigationDelegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
    [weWebView loadRequest:request];
    [self.view addSubview:weWebView];
}
#pragma mark - WebKitDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    
    NSString *path = [[navigationAction.request URL] absoluteString];
    
    path = [path stringByRemovingPercentEncoding];
    NSLog(@"urlString=%@",path);
    if ([path rangeOfString:@"protocol://orderstatus"].location != NSNotFound)
    {
        NSString *encodedValue = [[path substringFromIndex:23] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *JSONData = [encodedValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",responseJSON);
        NSString *trckingID = responseJSON[@"id"];
        NSString *PNoStr;
        if ([[NSString stringWithFormat:@"%@",responseJSON[@"Status"]] isEqualToString:@"0"]) // 抢单了
        {
            PNoStr = @"1005";
            
        }
        else if ([[NSString stringWithFormat:@"%@",responseJSON[@"Status"]] isEqualToString:@"4"]) // 付款了
        {
            PNoStr = @"1013";
            
        }
        else
        {
            PNoStr = @"1007";
        }
        [[BaseSocketManager shareInstance] sendDataToSocket:trckingID PNo:PNoStr successBlock:^(id responseBody) {
            NSLog(@"%@",responseBody);
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",responseBody]];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    if ([path rangeOfString:@"protocol://open"].location != NSNotFound)
    {
        NSString *encodedValue = [[path substringFromIndex:16] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *JSONData = [encodedValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",responseJSON);
        JCKNoHearViewController *sub = [[JCKNoHearViewController alloc] init];
        sub.title = responseJSON[@"title"];
        sub.url = responseJSON[@"url"];
        sub.ishidden = YES;
        [self.navigationController pushViewController:sub animated:YES];
        
    }
    if ([path rangeOfString:@"reloadjs"].location != NSNotFound)
    {
        NSString *encodedValue = [[path substringFromIndex:20] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *JSONData = [encodedValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",responseJSON);
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"vmSiteshow" object:nil];
        
    }
    
    if ([path rangeOfString:@"reloadParentjs"].location != NSNotFound)
    {
        NSString *encodedValue = [[path substringFromIndex:20] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData *JSONData = [encodedValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",responseJSON);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addSuccess" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
        
    }

    decisionHandler(WKNavigationActionPolicyAllow);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{

    [SVProgressHUD dismiss];
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [SVProgressHUD dismiss];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    ErrorShow(@"加载失败");
}
#pragma mark - socketDelegate
- (void)reloData:(NSNotification *)obj
{
    NSLog(@"%@",obj.object);
    [weWebView reload];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
